﻿/// ****************************************************************************
/// Author:	        Robin Zhu
/// Create Date:	2013-11-4
/// Purpose: 		应用的功能集合
/// ****************************************************************************
/// Modify By		Date			Remark
/// Robin           2014-6-9        增加GetPermissionCodeByTicket方法
/// ****************************************************************************
///
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using RB.Domain.RelationalMapping;

namespace RB.Web.Security.Rdb
{
    /// <summary>
    /// 应用的功能集合
    /// </summary>
    public class PermissionSet : CachedKeyedRecordSet<Permission, string>
    {
        //public override bool Contains(Permission item)
        //{
        //    return this.Any(t => t.Code == item.Code);
        //}

        //public override bool Remove(Permission item, DataAccess.Executor exec)
        //{
        //    int count = this.RemoveAll(t => t.Code == item.Code, exec);
        //    DbAuthorizeService.ResetPermissionCache();
        //    return count > 0;
        //}

        //public override Permission this[string key]
        //{
        //    get
        //    {
        //        return this.FirstOrDefault(t => t.Code == key);
        //    }
        //    set
        //    {
        //        this.Update(t => value, t => t.Code == key);
        //    }
        //}

        //public override int Update<TField>(System.Linq.Expressions.Expression<Func<Permission, TField>> fields, System.Linq.Expressions.Expression<Func<Permission, bool>> match, DataAccess.Executor exec)
        //{
        //    var result = base.Update<TField>(fields, match, exec);
        //    DbAuthorizeService.ResetPermissionCache();
        //    return result;
        //}

        //public override void Add(Permission item, DataAccess.Executor exec)
        //{
        //    base.Add(item, exec);
        //    DbAuthorizeService.ResetPermissionCache();
        //}
        protected override string GetKey(Permission item)
        {
            return item.Code;
        }

        protected override Permission GetValue(string key)
        {
            return base.SingleOrDefault(p => p.Code == key);
        }

        protected override bool RemoveByKey(string key, DataAccess.Executor exec)
        {
            int n = base.RemoveAll(p => p.Code == key, exec);
            return n > 0;
        }

        protected override void SetValue(string key, Permission value)
        {
            base.Update(p => value, p => p.Code == key);
        }

        internal string[] GetPermissionCodeByTicket(string appCode, string ticket)
        {
            var q = from t in this.AsQueryable()
                    join t1 in (new RolePermissionSet()).AsQueryable() on t.Code equals t1.PermCode
                    join t2 in (new UserAccountRoleSet()).AsQueryable() on t1.RoleId equals t2.RoleId
                    join t3 in (new UserSessionSet()).AsQueryable() on t2.UserId equals t3.UserId
                    where t3.Ticket == ticket && t.AppCode == appCode
                    select t.Code;
            return q.ToArray();
        }
    }
}
